/*
 * PU_WeatherManager
 * 
 * Copyright 2008-2011 Centro Andaluz de Innovacion y Tecnologias de la
 * Informacion y las Comunicaciones (CITIC). All Rights Reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
 *
 * Please contact CITIC - Edificio CITIC, C/ Marie Curie, 6 Parque
 * Tecnologico de Andalucia 29590 - Campanillas, Malaga, Spain
 * or visit www.citic.es, if you need additional information or
 * have any questions.
 */

package es.citic.puedes.weatherservice.test.unit;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import junit.framework.TestCase;

import es.citic.puedes.weatherservice.IWeatherManager;
import es.citic.puedes.weatherservice.WeatherException;
import es.citic.puedes.weatherservice.impl.WeatherManagerImpl;
import es.citic.puedes.weatherservice.impl.bbdd.WeatherDataBase;
import es.citic.puedes.weatherservice.model.Language;
import es.citic.puedes.weatherservice.model.Location;
import es.citic.puedes.weatherservice.model.Weather;
import es.citic.puedes.weatherservice.model.WeatherCondition;
import es.citic.puedes.weatherservice.umm.WeatherPreferences;

public class PU_WeatherManager extends TestCase {

	private static final Integer DEFAULT_USER_ID = 1;
	private IWeatherManager weatherManager;
	private WeatherDataBase dataBase;

	public void setUp() throws Exception {

		// HSQLDB in memory config
		Map<String, String> properties = new HashMap<String, String>();
		properties
				.put("javax.persistence.jdbc.driver", "org.hsqldb.jdbcDriver");
		properties.put("javax.persistence.jdbc.url", "jdbc:hsqldb:mem:test");
		properties.put("javax.persistence.jdbc.user", "SA");
		properties.put("javax.persistence.jdbc.password", "");

		dataBase = new WeatherDataBase("WeatherManager_pu", properties);
		weatherManager = new WeatherManagerImpl("config.xml", dataBase);
	}

	public void tearDown() throws Exception {
		dataBase.cleanEntityManager();
	}

	public void testPU_01() throws SecurityException, WeatherException {
		final String cityName = "Fuengirola";
		List<Location> locations = weatherManager.getLocation(cityName);
		assertNotNull(locations);
		assertEquals(1, locations.size());
		assertEquals(cityName, locations.get(0).getLocation());
	}

	public void testPU_02() throws SecurityException, WeatherException {
		final String cityName = "Cordoba";
		List<Location> locations = weatherManager.getLocation(cityName);
		assertNotNull(locations);
		assertTrue(locations.size() > 0);
	}

	public void testPU_03() throws SecurityException, WeatherException {
		final String cityName = null;

		try {
			weatherManager.getLocation(cityName);
			fail();
		} catch (WeatherException ex) {
		}
	}

	public void testPU_04() throws SecurityException, WeatherException {
		final String cityName = "";

		try {
			weatherManager.getLocation(cityName);
			fail();
		} catch (WeatherException ex) {
		}
	}

	public void testPU_05() throws SecurityException, WeatherException {
		final String cityName = "dadasd";

		try {
			weatherManager.getLocation(cityName);
			fail();
		} catch (WeatherException ex) {
		}
	}

	public void testPU_06() throws SecurityException, WeatherException {
		final String cityName = "Toulouse";
		final String cityCountry = "France";

		List<Location> locations = weatherManager.getLocation(cityName);
		assertNotNull(locations);

		Location foundLocation = null;
		for (Location location : locations) {
			if (location.getLocation().contains(cityName)
					&& cityCountry.equalsIgnoreCase(location.getCountry())) {
				foundLocation = location;
				break;
			}
		}

		assertNotNull(cityName + " in " + cityCountry + " not found",
				foundLocation);
	}

	public void testPU_07() throws SecurityException, WeatherException {
		final String cityName = "Cordoba";
		List<Location> locations = weatherManager.getLocation(cityName);
		assertNotNull(locations);
		for (Location location : locations) {
			assertNotNull(location.getCountry());
			assertFalse(location.getCountry().trim().isEmpty());
			assertNotNull(location.getLocation());
			assertFalse(location.getLocation().trim().isEmpty());
			assertNotNull(location.getLatitude());
			assertFalse(location.getLatitude() == 0);
			assertNotNull(location.getLongitude());
			assertFalse(location.getLongitude() == 0);
			assertNotNull(location.getPopulation());
			assertTrue(location.getPopulation().toString(),
					location.getPopulation() >= 0);
		}
	}

	public void testPU_08() throws SecurityException, WeatherException {
		final String cityName = "Cordoba";
		final int days = 1;
		final Language language = Language.SPANISH;

		List<Location> locations = weatherManager.getLocation(cityName);
		Location location = locations.get(0);

		WeatherCondition weatherCondition = weatherManager.getWeather(
				location.getLocation(), location.getCountry(), days, language,
				DEFAULT_USER_ID);

		assertTrue(weatherCondition.getLocation().contains(cityName));
		assertEquals(location.getCountry(), weatherCondition.getCountry());
		assertNotNull(weatherCondition.getWeatherCondition());
		assertTrue(weatherCondition.getWeatherCondition().size() > 0);

		for (Weather weather : weatherCondition.getWeatherCondition()) {
			assertNotNull(weather.getDate());
			assertFalse(weather.getDate().trim().isEmpty());
			assertNotNull(weather.getDescription());
			assertFalse(weather.getDescription().trim().isEmpty());
			assertNotNull(weather.geticonURL());
			assertFalse(weather.geticonURL().trim().isEmpty());
			assertNotNull(weather.getPrecipitation());
			assertTrue(weather.getPrecipitation().toString(),
					weather.getPrecipitation() >= 0.0d);
			assertNotNull(weather.getTemperature());
			assertNotNull(weather.getWind());
		}
	}

	public void testPU_09() throws SecurityException, WeatherException {
		final int days = 1;
		final Language language = Language.SPANISH;

		try {
			weatherManager.getWeather(null, null, days, language,
					DEFAULT_USER_ID);
			fail();
		} catch (WeatherException ex) {
		}
	}

	public void testPU_10() throws SecurityException, WeatherException {
		final String cityName = "Cordoba";
		final int days = 8;
		final Language language = Language.SPANISH;

		List<Location> locations = weatherManager.getLocation(cityName);
		Location location = locations.get(0);

		try {
			weatherManager.getWeather(location.getLocation(),
					location.getCountry(), days, language, DEFAULT_USER_ID);
			fail();
		} catch (WeatherException ex) {
		}
	}

	public void testPU_11() throws SecurityException, WeatherException {
		List<WeatherPreferences> preferences = weatherManager.getPreferences(
				DEFAULT_USER_ID, Language.SPANISH);
		assertEquals(0, preferences.size());
	}
}
